/*
 * Copyright 2011 Angel Sanadinov
 *
 * This file is part of VBox WMI.
 *
 * VBox WMI is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VBox WMI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
 */

package Beans.Responses.DataRetrievalComponents;

import Beans.ServerBean;
import Utilities.Constants;
import Utilities.Functions;

/**
 * This class represents server data, when retrieved from the system.
 *
 * @author Angel Sanadinov
 */
public class ServerDataComponent extends DataRetrievalComponent
{
    private ServerBean serverData;      //server data object
    private boolean isAccessLimited;    //denotes whether access to the server data should be limited
    private int serverId;               //the id of the server *
    private String serverName;          //the name of the server *
    private String serverDescription;   //the server description *
    private String serverAddress;       //the address of the server *
    //* -> for limited access only

    /**
     * Construct a server data object, based on the supplied data.
     *
     * @param serverData server data
     * @param isAccessLimited set to <code>true</code> if access to the server data
     *                        should be limited
     */
    public ServerDataComponent(ServerBean serverData, boolean isAccessLimited)
    {
        if(isAccessLimited)
        {
            this.serverData = null;
            this.serverId = serverData.getServerId();
            this.serverName = serverData.getName();
            this.serverDescription = serverData.getDescription();
            this.serverAddress = serverData.getWebServiceAddress();
        }
        else
        {
            this.serverData = serverData;
            this.serverId = Constants.INVALID_SERVER_ID;
            this.serverName = null;
            this.serverDescription = null;
            this.serverAddress = null;
        }
        
        this.isAccessLimited = isAccessLimited;
    }

    /**
     * Retrieves the server data object.
     *
     * @return the server data, if available or <code>null</code> otherwise
     */
    public ServerBean getServerData()
    {
        return serverData;
    }

    /**
     * Returns whether access to the server data is limited.
     *
     * @return <code>true</code> if access is limited or <code>false</code> otherwise
     */
    public boolean isAccessLimited()
    {
        return isAccessLimited;
    }

    /**
     * Retrieves the id of the server.
     *
     * @return the server id
     */
    public int getServerId() 
    {
        if(isAccessLimited)
            return serverId;
        else
            return serverData.getServerId();
    }

    /**
     * Retrieves the name of the server.
     *
     * @return the server name
     */
    public String getServerName()
    {
        if(isAccessLimited)
            return serverName;
        else
            return serverData.getName();
    }

    /**
     * Retrieves the server's description.
     *
     * @return the server description
     */
    public String getServerDescription()
    {
        if(isAccessLimited)
            return serverDescription;
        else
            return serverData.getDescription();
    }

    /**
     * Retrieves the server's web service address.
     *
     * @return the server's address
     */
    public String getServerAddress()
    {
        if(isAccessLimited)
            return serverAddress;
        else
            return serverData.getWebServiceAddress();
    }

    @Override
    public boolean isValid()
    {
        if((isAccessLimited && Functions.isServerIdValid(serverId) && serverName != null && serverDescription != null)
           || (!isAccessLimited && serverData != null && serverData.isValid()))
            return true;
        else
            return false;
    }

    @Override
    public String toString()
    {
        return isAccessLimited + "," + serverId + "," + serverName + "," + serverDescription;
    }
}
